﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using GUI.src;
using Microsoft.Win32;
using treadmillProject.src.model;
using Path = System.IO.Path;

namespace GUI {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private TrainingProgramGraph trainingProgramGraph;
        public PulseGraph pulseGraph;
        public SpeedGraph speedGraph;
        public StatusPanel statusPanel;
        private Thread myThread;

        private TrainingSession myTrainingSession;
        private static int temp = 0;

        private Label[] statusPanelLabelArray = new Label[6];


        public MainWindow() {
            InitializeComponent();
            Loaded += MainWindowLoaded;
        }

        public void thread_func() {
            while (true) {
                myTrainingSession.recv();
                Dispatcher.Invoke(notifyAll);
            }
        }

        private void notifyAll() {
            pulseGraph.OnUpdate();
            speedGraph.OnUpdate();
            statusPanel.OnUpdate();
        }


        private
            void MainWindowLoaded
            (object sender, RoutedEventArgs e) {
            trainingProgramGraph = new TrainingProgramGraph(trainingProgramGraphChart);
            statusPanelLabelArrayInit();
            pulseGraph = new PulseGraph(pulseGraphChart);
            speedGraph = new SpeedGraph(speedGraphChart);
            statusPanel = new StatusPanel(statusPanelLabelArray);

            myTrainingSession = TrainingSession.accessTrS();

            myThread = new Thread(thread_func);
            myThread.Start();
            myThread.Suspend();
            myThread.IsBackground = true;
        }


        private void statusPanelLabelArrayInit() {
            statusPanelLabelArray[0] = LbCurrentPulse;
            statusPanelLabelArray[1] = LbCurrentSpeed;
            statusPanelLabelArray[2] = LbCurrentGear;

            statusPanelLabelArray[3] = LbMaxPulse;
            statusPanelLabelArray[4] = LbMaxSpeed;
            statusPanelLabelArray[5] = LbMaxGear;
        }

        private
            void Open_Click
            (object sender, RoutedEventArgs e) {
            Console.WriteLine("Loading...");

            OpenFileDialog oFD = new OpenFileDialog();

            SaveFileDialog sFD = new SaveFileDialog();
            oFD.Title = "save TrainingData";
            //            oFD.InitialDirectory = @"Desktop";

            oFD.FileName = "TrainingData"; // Default file name
            oFD.DefaultExt = ".osl"; // Default file extension
            oFD.Filter = "Text documents (.osl)|*.osl"; // Filter files by extension // Show open file dialog box
            if (oFD.ShowDialog() == true) {
                myTrainingSession.ReStore(oFD.FileName);

                notifyAll();
            }
        }


        private
            void Save_Click
            (object sender, RoutedEventArgs e) {
            Console.WriteLine("Saving...");

            SaveFileDialog sFD = new SaveFileDialog();
            sFD.Title = "save TrainingData";
            //            oFD.InitialDirectory = @"Desktop";

            sFD.FileName = "TrainingData"; // Default file name
            sFD.DefaultExt = ".osl"; // Default file extension
            sFD.Filter = "Text documents (.osl)|*.osl"; // Filter files by extension // Show open file dialog box

            if (sFD.ShowDialog() == true) {
                myTrainingSession.Store(sFD.FileName);
            }
        }

        private
            void MIAbout_Click
            (object sender, RoutedEventArgs e) {
            MessageBox.Show("TreadMill Proj v1\n C# Course 2012-13");
        }

        private
            void ConnectToServer_Click
            (object sender, RoutedEventArgs e) {
            myTrainingSession.InitiateConnection();

            myThread.Resume();
            CloseConnection.IsEnabled = true;
            ConnectToServer.IsEnabled = false;
            BtStart.IsEnabled = true;
        }

        private
            void CloseConnection_Click
            (object sender, RoutedEventArgs e) {
            if (myThread.IsAlive) {
                myThread.Suspend();
                myTrainingSession.EndConnection();
            }

            CloseConnection.IsEnabled = false;
            ConnectToServer.IsEnabled = true;
            BtStart.IsEnabled = false;
            BtStop.IsEnabled = false;
        }

        private
            void onStart_Click
            (object sender, RoutedEventArgs e) {
            myTrainingSession.sendStart();
            BtStart.IsEnabled = false;
            BtStop.IsEnabled = true;
        }

        private
            void onStop_Click
            (object sender, RoutedEventArgs e) {
            myTrainingSession.sendStop();
            BtStart.IsEnabled = true;
            BtStop.IsEnabled = false;
        }

        private
            void onReset_Click
            (object sender, RoutedEventArgs e) {
            myTrainingSession.resetTrainingData();
            notifyAll();
        }

        private
            void onExit_Click
            (object sender, RoutedEventArgs e) {
            Environment.Exit(0);
        }


        private
            void GraphsVisible
            (object sender, RoutedEventArgs e) {
            // checks for pulseGraph
            if (CBPulseGraph.IsChecked == true) {
                pulseGraphChart.Visibility = Visibility.Visible;
            } else if (CBPulseGraph.IsChecked == false) {
                pulseGraphChart.Visibility = Visibility.Hidden;
            }

            // checks for SpeedGraph
            if (CBSpeedGraph.IsChecked == true) {
                speedGraphChart.Visibility = Visibility.Visible;
            } else if (CBSpeedGraph.IsChecked == false) {
                speedGraphChart.Visibility = Visibility.Hidden;
            }

            // check got CBTrainingProgramGraph
            if (CBTrainingProgramGraph.IsChecked == true) {
                trainingProgramGraphChart.Visibility = Visibility.Visible;
                //trainingProgramGraphChartHead.Visibility = Visibility.Visible;
            } else if (CBTrainingProgramGraph.IsChecked == false) {
                trainingProgramGraphChart.Visibility = Visibility.Hidden;
                //trainingProgramGraphChartHead.Visibility = Visibility.Hidden;
            }
        }

        // MI for MenuItem
        private
            void MISelectTrainingSession
            (object sender, RoutedEventArgs e) {
            trainingProgramGraph.LoadTrainingProgram();
        }
    }
}